package ntut.andengine.game;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel15 extends GameLevel {

GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {
		
        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(240, 680));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel15.this, GameLevel13.class);
				}
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {}
		
		});
        
        PhysicalFactory.CreateWall(this, new Vector2(20, 481), 20, 120, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(80, 440), 80, 40, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(100, 500), 40, 80, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(180, 500), 40, 80, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(260, 480), 40, 120, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(300, 520), 40, 40, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(180, 440), 100, 40, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(350, 479), 20, 120, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(420, 440), 100, 40, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(420, 500), 40, 80, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(240, 380), 480, 80, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(140, 440), 280, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(401, 440), 160, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(240, 580), 480, 80, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(20, 520), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(100, 520), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(180, 520), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(300, 520), 120, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(420, 520), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(20, 640), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(100, 640), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(180, 640), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(260, 640), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(340, 640), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(420, 640), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(460, 320), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(380, 320), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(300, 320), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(220, 320), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(140, 320), 40, 40, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(60, 320), 40, 40, ComponentColor.Blue);
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(240, 80), ComponentColor.Blue);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	

		return mScene;
	}
}
